#
# 配置/home/cmh/.bashrc为登录启动
# 配置/etc/rc.local为开机启动无效
# 配置irremote 32 gpio12 在系统中/boot/config.txt
#
import evdev
import RPi.GPIO as GPIO
import time
from .motor import *
from .led import *

def get_ir_device():
    devices = [evdev.InputDevice(path) for path in evdev.list_devices()]
    for device in devices:
        if (device.name == "gpio_ir_recv"):
            return device


def irremote_worker():
    """irremote thread worker function"""
    device = get_ir_device()
    while True:
        time.sleep(0.0001)
        next_event = device.read_one()
        if (next_event):
            if (next_event.value == 24):  # 2
                led_go()
                car_go()
            elif (next_event.value == 8):
                led_turn_right()
                car_turn_right()
            elif (next_event.value == 90):
                led_turn_left()
                car_turn_left()
            elif (next_event.value == 82):  # 8
                led_back()
                car_back()
            elif (next_event.value == 28):  # 5
                led_stop()
                car_stop()
